﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Albon.Model;
namespace BLL
{
    public class PayMentBLL
    {
        private readonly DAL.PayMentDAL paymentdal = new DAL.PayMentDAL();
        
        /// <summary>
        /// 添加：发票ID,付款数，接收人，备注
        /// 修改：只能修改钱数和备注
        /// 返回应付款和已付款的差值
        /// </summary>
        public void addPayMent(PayMent payment)
        {
            paymentdal.addPayMent(payment);
            if (Balance(payment.invoiceNo.ToString()) >= 0)
            {
                OrderBLL orderbll = new OrderBLL();
                orderbll.putHoudOrderInWork(payment.invoiceNo);
            }
        }

        /// <summary>
        /// 每次付款完成后判断当前订单的付款是否已经完成
        /// </summary>
        public decimal Balance(string invoiceNo)
        {
            decimal totalPayment = 0;
            decimal totalMoney = 0;
            DataTable allPayment = paymentdal.allPayMent(invoiceNo);
            foreach (DataRow dr in allPayment.Rows)
            {
                totalPayment += decimal.Parse(dr["payMoney"].ToString());
            }
            InvoiceBLL invoicebll = new InvoiceBLL();
            totalMoney = invoicebll.getTotalMoney(invoiceNo);
            return  totalPayment-totalMoney ;
        }

        /// <summary>
        /// 删除一条付款信息
        /// </summary>
        public bool deletePayMent(string paymentID)
        {
            return paymentdal.deletePayMent(paymentID);
        }
    }
}
